package com.yunshipei.ui.activity;

import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.core.common.XCloudException;
import com.yunshipei.core.utils.FileUtils;
import com.yunshipei.enterplorer.R;
import com.yunshipei.ui.dialog.DialogHelper;
import com.yunshipei.ui.dialog.WaitDialog;
import com.yunshipei.utils.ToastUtils;

import org.reactivestreams.Publisher;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class PreviewLogActivity extends BaseActivity {

    private static final String LOG_FILE_NAME = "dynamic.log";
    private static final String LOG_ROOT_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + "com.ysp.xcloud" + File.separator + "log";

    @Bind(R.id.log_web_view)
    protected WebView mWebView;

    private WaitDialog mWaitDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_log);
        ButterKnife.bind(this);
        WebSettings settings = mWebView.getSettings();
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        mWaitDialog = DialogHelper.getNoCancelableWaitDialog(this, "加载本地日志...");
        mWaitDialog.show();
        File logFile = new File(LOG_ROOT_PATH, LOG_FILE_NAME);
        Flowable.just(logFile).flatMap(new Function<File, Publisher<String>>() {
            @Override
            public Publisher<String> apply(File file) throws Exception {
                String content = FileUtils.readFile(file.getAbsolutePath());
                if (TextUtils.isEmpty(content)) {
                    return Flowable.error(new XCloudException("没有可查看日志"));
                }
                return Flowable.just(content);
            }
        }).subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                mWaitDialog.dismiss();
                mWebView.loadData(s, "text/html; charset=UTF-8", null);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mWaitDialog.dismiss();
                ToastUtils.showToast(throwable.getMessage());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.stopLoading();
            mWebView.destroy();
        }
    }
}
